﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sylvain.Manager;
using Sylvain.Model;


public partial class Admin_EditNewsSection : System.Web.UI.Page
{
    //Instance of the SylvainManager Class
    ISylvainManager objManger = new SylvainManager();
    /// <summary>
    /// Bind Dropdownlist with Enums
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            string[] names = Enum.GetNames(typeof(Tag));
            Array values = Enum.GetValues(typeof(Tag));
            ddlNews.Items.Insert(0,"--Select Item--");
            for (int i = 0; i <= names.Length-1 ; i++)
            {
                ddlNews.Items.Add(new ListItem(names[i], Convert.ToInt32(values.GetValue(i)).ToString()));
            }
          //Bind NewsSection Details with Gridview
          BindNewsSectionGrid();
        }
    }
    /// <summary>
    /// Bind NewsSection Details with Gridview
    /// </summary>
    protected void BindNewsSectionGrid()
    {

        grvNewsSection.DataSource = objManger.GetAllNews();
        grvNewsSection.DataBind();

    }
    /// <summary>
    /// Create/Add New Record in NewsSection
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            NewsSection objNews = new NewsSection();
            if (ddlNews.SelectedIndex > 0 && txtTitle.Text!="" && FCKeditor.Value!="")
            {
                objNews.Tag = ddlNews.SelectedIndex;
                objNews.Title = txtTitle.Text.Trim();
                objNews.Description = FCKeditor.Value;
                objNews.CreatedDate = System.DateTime.Now;
                objNews.UpdatedDate = System.DateTime.Now;
              

                // insert News
                objManger.AddNewsSection(objNews);


                BindNewsSectionGrid();
                ddlNews.SelectedIndex = 0;
                FCKeditor.Value = "";
                txtTitle.Text = "";
                lblError.Text = "Submitted Successfully";
            }
             else {
                    if(ddlNews.SelectedIndex==0)
                    {
                     lblError.Text = "Please Select an Item.";
                    }
                    else if(txtTitle.Text=="")
                    {
                        lblError.Text = "Title is required.";
                    }
                    else if (FCKeditor.Value == "")
                    {
                        lblError.Text = "Description is required.";
                    }
                    else
                    {
                        lblError.Text ="";
                    }
            }
        }
        catch
        {
            lblError.Text = "There is some error. please try again.";
        }

    }
    /// <summary>
    /// Update Record in NewsSection
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {

            NewsSection objUpdate = new NewsSection();
            if (ddlNews.SelectedIndex > 0)
            {
                objUpdate.Tag = ddlNews.SelectedIndex;
                objUpdate.Id = int.Parse(hidId.Value);
                objUpdate.Title = txtTitle.Text.Trim();
                objUpdate.Description = FCKeditor.Value;
                objUpdate.UpdatedDate = System.DateTime.Now;



                // Update NewsSection
                objManger.EditNewsSection(objUpdate);
                //bind News Section grid
                BindNewsSectionGrid();

                lblError.Text = "Updated Successfully";
                ddlNews.SelectedIndex = 0;
                FCKeditor.Value = "";
                txtTitle.Text = "";

                btnUpdate.Visible = false;
                btnSubmit.Visible = true;
            }
            else {
                lblError.Text = "Please Select an Item.";
            }

        }
        catch
        {
            lblError.Text = "There is some error. please try again.";
        }
    }
    /// <summary>
    /// Delete/Update Record in Gridview
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvNewsSection_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Delete")
            {
                int ID = Convert.ToInt32(e.CommandArgument);

                //Delete News
                objManger.DeleteNewsSection(ID);
                BindNewsSectionGrid();
          
            }
            if (e.CommandName == "Update")
            {
                hidId.Value = e.CommandArgument.ToString();
            
                btnSubmit.Visible = false;
                btnUpdate.Visible = true;

                lblError.Text = "";
            }
        }
        catch
        {
            lblError.Text = "Operation could not be performed.";
        }
    }
    /// <summary>
    /// Updating a Record
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvNewsSection_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            GridViewRow row = (GridViewRow)grvNewsSection.Rows[e.RowIndex];
            FCKeditor.Value = ((Label)row.FindControl("lblDescription")).Text;
            txtTitle.Text = ((Label)row.FindControl("lblTitle")).Text;
            ddlNews.SelectedIndex =Convert.ToInt32( ((HiddenField)row.FindControl("hdnSelectedItem")).Value);
            btnSubmit.Visible = false;
            btnUpdate.Visible = true;
        }
        catch
        {
            lblError.Text = "There is some error. please try again.";
        }
    }
    /// <summary>
    /// Display Page Index
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvNewsSection_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvNewsSection.PageIndex = e.NewPageIndex;
        BindNewsSectionGrid();
    }
    /// <summary>
    /// Deleting a Rocord
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvNewsSection_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
   
}